<template>

  <!-- 新增弹框 -->
  <el-dialog title="新增弹层" :visible="show" @close="close" @open="open">
    <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="roleForm.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="roleForm.description" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small">取消</el-button>
        <el-button size="small" type="primary" @click="addok">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { xinzeng, xiugaijuese, xiangxixinxi } from '@/api/setting'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {

      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 打开时回调
    async open() {
      // 判断是否有id
      if (this.id) {
        const res = await xiangxixinxi(this.id)
        this.roleForm = res
      }
    },
    // 关闭回调
    close(success = 0) {
      this.$emit('close', success)
      this.roleForm = {
        name: '',
        description: ''
      }
      // 重置表单校验
      this.$refs.roleForm.resetFields()
    },
    // 确定
    addok() {
      this.$refs.roleForm.validate(async(val) => {
        if (val) {
          // console.log('id', this.id)
          if (this.id) {
            // 编辑
            await xiugaijuese(this.roleForm)
            // console.log('编辑', this.roleForm)
          } else {
            // 新增
            await xinzeng(this.roleForm)
            // console.log('新增', this.roleForm)
          }
          this.close(1)
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>

</style>
